package com.gtw.modules.wx;

import com.gtw.modules.api.controller.weixinController;
import com.gtw.modules.wx2.modules.util.IOUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wuye on 2018/5/22.
 */
public class MessageUtil {

    /**
     * 返回消息类型：文本
     */
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";

    /**
     * 返回消息类型：音乐
     */
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";

    /**
     * 返回消息类型：图文
     */
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";

    /**
     * 请求消息类型：文本
     */
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";

    /**
     * 请求消息类型：图片
     */
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";

    /**
     * 请求消息类型：链接
     */
    public static final String REQ_MESSAGE_TYPE_LINK = "link";

    /**
     * 请求消息类型：地理位置
     */
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";

    /**
     * 请求消息类型：音频
     */
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";

    /**
     * 请求消息类型：推送
     */
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";


    /**
     * 扫码二维码事件
     */
    public static final Object EVENT_TYPE_SCAN = "SCAN";


    /**
     * 事件类型：subscribe(订阅)
     */
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";

    /**
     * 事件类型：unsubscribe(取消订阅)
     */
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";

    /**
     * 事件类型：CLICK(自定义菜单点击事件)
     */
    public static final String EVENT_TYPE_CLICK = "CLICK";


    /**
     * 解析微信发来的请求（XML） xml示例 <xml> <ToUserName><![CDATA[toUser]]></ToUserName>
     * <FromUserName><![CDATA[FromUser]]></FromUserName>
     * <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType>
     * <Event><![CDATA[CLICK]]></Event>
     * <EventKey><![CDATA[EVENTKEY]]></EventKey> </xml>
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXmlToMap(HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        ServletInputStream inputStream = null;
        try {
            // 从request中取得输入流
            inputStream = request.getInputStream();
            // 读取输入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
            }
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXmlToMap2(String body){
        Map<String, String> map = new HashMap<>();
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(body.toString().getBytes("UTF-8")));
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
            }
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 解析微信发来的请求（XML） xml示例 <xml> <ToUserName><![CDATA[toUser]]></ToUserName>
     * <FromUserName><![CDATA[FromUser]]></FromUserName>
     * <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType>
     * <Event><![CDATA[CLICK]]></Event>
     * <EventKey><![CDATA[EVENTKEY]]></EventKey> </xml>
     *
     * @param
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseStreamToMap(InputStream inputStream) throws Exception {
        Map<String, String> map = new HashMap<>();
        try {
            // 读取输入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 使用dom4将xml文件中的数据转换成TreeMap<Object,Object>
     *
     * @param xmlString xml格式的字符串
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static TreeMap<String, Object> parseXmlToTreeMap(String xml, String encoding)
            throws ParserConfigurationException, IOException, SAXException {
        // 这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream is = IOUtil.toInputStream(xml, encoding);
        org.w3c.dom.Document document = builder.parse(is);
        // 获取到document里面的全部结点
        org.w3c.dom.NodeList allNodes = document.getFirstChild().getChildNodes();
        org.w3c.dom.Node node;
        TreeMap<String, Object> map = new TreeMap<>();
        int i = 0;
        while (i < allNodes.getLength()) {
            node = allNodes.item(i);
            if (node instanceof org.w3c.dom.Element) {
                map.put(node.getNodeName(), node.getTextContent());
            }
            i++;
        }
        return map;
    }

    /**
     * 使用dom4将xml文件中的数据转换成Map<Object,Object>
     *
     * @param xmlString xml格式的字符串
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static Map<String, Object> parseXmlToMap(String xml, String encoding)
            throws ParserConfigurationException, IOException, SAXException {
        // 这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream is = IOUtil.toInputStream(xml, encoding);
        org.w3c.dom.Document document = builder.parse(is);
        // 获取到document里面的全部结点
        org.w3c.dom.NodeList allNodes = document.getFirstChild().getChildNodes();
        org.w3c.dom.Node node;
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        while (i < allNodes.getLength()) {
            node = allNodes.item(i);
            if (node instanceof org.w3c.dom.Element) {
                map.put(node.getNodeName(), node.getTextContent());
            }
            i++;
        }
        return map;
    }

    /**
     * 示例 <xml> <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
     * <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>
     * <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
     * <trade_type><![CDATA[JSAPI]]></trade_type> </xml>
     *
     * 将xml数据(<![CDATA[SUCCESS]]>格式)映射到java对象中
     *
     * @param xml
     *            待转换的xml格式的数据
     * @param t
     *            待转换为的java对象
     * @return
     */
    public static <T> T getObjectFromXML(String xml, Class<T> t) {
        // 将从API返回的XML数据映射到Java对象
        XStream xstream = XStreamFactroy.init(true);
        xstream.alias("xml", t);
        xstream.ignoreUnknownElements();// 暂时忽略掉一些新增的字段
//		if(Objects.equals(xstream.fromXML(xml).getClass(), t.getClass())) {
//			return t.cast(xstream.fromXML(xml));
//		}
//		return null;
        return t.cast(xstream.fromXML(xml));
    }

    /**
     * 将java对象转换为xml(<![CDATA[SUCCESS]]>格式)
     *
     * @param obj
     * @return
     */
    public static String toXml(Object obj) {
        String result = "";
        XStream xstream = XStreamFactroy.init(true);
        xstream.alias("xml", obj.getClass());
        result = xstream.toXML(obj);
        return result;
    }

    /**
     * 将java对象转换为xml文件,并去除 _ 应用场景是 去除实体中有_划线的实体, 默认会有两个_,调用该方法则会去除一个
     *
     * @param obj
     * @return
     */
    public static String toSplitXml(Object obj) {
        String result = "";
        XStream xstream = XStreamFactroy.initSplitLine();
        xstream.alias("xml", obj.getClass());
        result = xstream.toXML(obj);
        return result;
    }

    /**
     * XStream工具类
     * @author phil
     *
     */
    static class XStreamFactroy {

        private static final String START_CADA = "<![CDATA[";
        private static final String END_CADA = "]]>";

        /**
         * 是否启用<![CDATA[]]>
         *
         * @param flag  true 表示启用 false表示不启用
         * @return
         */
        static XStream init(boolean flag) {
            XStream xstream = null;
            if (flag) {
                xstream = new XStream(new XppDriver() {
                    public HierarchicalStreamWriter createWriter(Writer out) {
                        return new PrettyPrintWriter(out) {
                            @Override
                            protected void writeText(QuickWriter writer, String text) {
                                if (!text.startsWith(START_CADA)) {
                                    text = START_CADA + text + END_CADA;
                                }
                                writer.write(text);
                            }
                        };
                    }
                });
            } else {
                xstream = new XStream();
            }
            return xstream;
        }

        /**
         * 用于处理在实体对象中带有_的属性,如果用上述方法，会出现有两个__,导致结果不正确! 属性中有_的属性一定要有改方法
         *
         * @return 返回xstream 对象 new DomDriver("UTF-8", new
         *         XmlFriendlyNameCoder("-_", "_")
         */
        public static XStream initSplitLine() {
            XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
            return xstream;
        }
    }


    public static void main(String[] args){
            String xmlString = "<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[FromUser] ]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType>< ![CDATA[event] ]></MsgType> <Event>< ![CDATA[SCAN] ]></Event> <EventKey>< ![CDATA[SCENE_VALUE] ]></EventKey> <Ticket>< ![CDATA[TICKET] ]></Ticket> </xml>";




            //去掉前后的xml标签
            xmlString = xmlString.replaceAll("</?xml>", "");
            System.out.println(xmlString);
            System.out.println("==========++++++++++++++");
            //匹配一段一段这样的数据   <attach><![CDATA[支付测试]]></attach>
            Pattern pattern = Pattern.compile("<.*?/.*?>");
            Matcher matcher = pattern.matcher(xmlString);
            //配置是否包含<![CDATA[CNY]]> CDATA 包裹的数据
            Pattern pattern2 = Pattern.compile("!.*]");
            Map<String, String> map = new HashMap<>();
            while(matcher.find()) {
                //获取键
                String key = matcher.group().replaceAll(".*/", "");
                key = key.substring(0, key.length() - 1);
                Matcher matcher2 = pattern2.matcher(matcher.group());
                String value = matcher.group().replaceAll("</?.*?>", "");
                //获取值
                if(matcher2.find() && !value.equals("DATA")) {
                    value = matcher2.group().replaceAll("!.*\\[", "");
                    value = value.substring(0, value.length() - 2);
                }
                map.put(key, value);
            }
            System.out.println(map);
        }
}
